<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */
class ManageController extends AdminController {

    public function index(){
        $where['status']=1;
        $list=M('skill')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=M('skill')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();


        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '才艺列表';
        $this->display();
    }



    //添加才艺
    public function skill_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            $data['name']=$_POST['name'];
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('skill')->where($where)->save($data);
            }else{

                $res=M('skill')->add($data);
            }

            if($res){
                $this->success("操作成功",U("index"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '才艺列表';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('skill')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }

    //删除才艺
    public  function  skill_del(){
        $map['id']=$_GET['id'];
        $data['status']=2;
        $res=M('skill')->where($map)->save($data);
        if($res){
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败");
        }
    }




    //礼物分类
    public function giftcate(){
        $where['status']=1;
        $list=M('giftcate')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=M('giftcate')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '礼物分类';
        $this->display();
    }





    //添加礼物分类
    public function giftcate_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            $data['name']=$_POST['name'];
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('giftcate')->where($where)->save($data);
            }else{

                $res=M('giftcate')->add($data);
            }

            if($res){
                $this->success("操作成功",U("giftcate"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '才艺分类';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('giftcate')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除礼物分类
    public  function  giftcate_del(){
        $map['id']=$_GET['id'];
        $data['status']=2;
        $res=M('giftcate')->where($map)->save($data);
        if($res){
            $this->success("删除成功",U("giftcate"));
        }else{
            $this->error("删除失败");
        }
    }




    //房间分类
    public function roomcate(){
        $where['status']=1;
        $list=M('roomcate')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=M('roomcate')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '房间分类';
        $this->display();
    }





    //添加礼物分类
    public function roomcate_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            $data['name']=$_POST['name'];
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('roomcate')->where($where)->save($data);
            }else{

                $res=M('roomcate')->add($data);
            }

            if($res){
                $this->success("操作成功",U("roomcate"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '房间分类';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('roomcate')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除礼物分类
    public  function  roomcate_del(){
        $map['id']=$_GET['id'];
        $data['status']=2;
        $res=M('roomcate')->where($map)->save($data);
        if($res){
            $this->success("删除成功",U("roomcate"));
        }else{
            $this->error("删除失败");
        }
    }


    //守护分类
    public function shlevel(){
        $where['status']=1;
        $where['type']=1;
        $list=M('sh_level')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=M('sh_level')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '守护分类';
        $this->display();
    }





    //添加守护分类
    public function shlevel_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            if(!$_POST['month']){
                $this->error("请输入开通时长");
            }
            if(!isint($_POST['month'])){
                $this->error("请输入开通时长数");
            }
            $data['name']=$_POST['name'];
            $data['month']=$_POST['month'];
            $data['img']=$_POST['img'];
            $data['type']=1;
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('sh_level')->where($where)->save($data);
            }else{
                $res=M('sh_level')->add($data);
            }

            if($res){
                $this->success("操作成功",U("shlevel"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '守护分类';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('sh_level')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


//    //删除守护分类
//    public  function  shlevel_del(){
//        $map['id']=$_GET['id'];
//        $res=M('shlevel')->where($map)->delete();
//        if($res){
//            $this->success("删除成功",U("shlevel"));
//        }else{
//            $this->error("删除失败");
//        }
//    }


    //快递类型
    public function express(){
        $where['status']=1;
        $where['type']=1;
        $list=M('express')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=M('express')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '快递类型';
        $this->display();
    }



    //添加快递类型
    public function express_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            if(!$_POST['code']){
                $this->error("请输入快递公司代码");
            }
            $data['name']=$_POST['name'];
            $data['code']=$_POST['code'];
            $data['type']=1;
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('express')->where($where)->save($data);
            }else{
                $res=M('express')->add($data);
            }

            if($res){
                $this->success("操作成功",U("express"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '快递类型';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('express')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除快递类型
    public  function  express_del(){
        $map['id']=$_GET['id'];
        $data['status']=2;
        $res=M('express')->where($map)->save($data);
        if($res){
            $this->success("删除成功",U("express"));
        }else{
            $this->error("删除失败");
        }
    }

    //退货退款类型
    public function refusecate(){
        $where['status']=1;
        $where['type']=1;
        $list=M('refusecate')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $list=M('refusecate')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '退货退款类型';
        $this->display();
    }





    //添加退货退款类型
    public function refusecate_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            $data['name']=$_POST['name'];
            $data['type']=1;
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('refusecate')->where($where)->save($data);
            }else{
                $res=M('refusecate')->add($data);
            }

            if($res){
                $this->success("操作成功",U("refusecate"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '退货退款类型';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('refusecate')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除退货退款类型
    public  function  refusecate_del(){
        $map['id']=$_GET['id'];
        $data['status']=2;
        $res=M('refusecate')->where($map)->save($data);
        if($res){
            $this->success("删除成功",U("refusecate"));
        }else{
            $this->error("删除失败");
        }
    }

    //认证列表
    public function suthentic(){
        $where['rz_status']=array('neq',0);
        $list=M('user')->where($where)->field('id,realname,mobile,rz_province,rz_city,rz_area,rz_detail,qqnum,email,idnum,skill_id,identity_img,rz_status,rz_time')->where($where)->order('id desc')->select();
       // dump($list);
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('user')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        foreach($list as $k=>$v){
            $map['id']=$v['skill_id'];
            $list[$k]['skill']=M('skill')->where($map)->getField('name');
        }
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '认证列表';
        $this->display();
    }

    //主播列表
    public function anchor(){
     $dataArr = cookie('anchor_search');
        if(!$_REQUEST['types']){
            cookie('anchor_search',null);
        }else{
            $dataArr = cookie('anchor_search');
        }
        $nickname=$dataArr['nickname'];

      // dump($nickname);
      //  $type=$dataArr['type'];
        if($nickname){
            $this->assign('nickname',$nickname);
            $where['nickname']=array('like','%'.$dataArr['nickname'].'%');
        }

        $where['rz_status']=array('eq',3);
        $where['is_anchor']=array('eq',1);

        $list=M('user')->where($where)->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('user')->field('id,nickname,number')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();

        foreach($list as $k=>$v){
            $bizId = C('ZB_BIZID').'_';
            $chaneel_id=$bizId.$v['number'];
            $info=singer_channel($chaneel_id);
            $info=json_decode($info)->output;
            if($info){
                if($info[0]->status==1){
                    $list[$k]['live_status']=1;
                }else{
                    $list[$k]['live_status']=0;
                }
            }else{
                $list[$k]['live_status']=0;
            }

            $gifts=M('send_gift')->where("auid=".$v['id'])->select();
            $all_zsnum = 0;
            foreach($gifts as $i=>$j){
                $all_zsnum += $j['num']*$j['zs_num'];//主播收到的钻石数
            }
            $list[$k]['total']=$all_zsnum;
            $gift_num = M('send_gift')->where("auid=".$v['id'])->sum('num');
            $list[$k]['gift_num']= isset($gift_num)?$gift_num:0;
            $info = M('anchor')->field('count,is_one')->where(['auid' => $v['id']])->find();
            $list[$k]['view_num']= isset($info['count'])?$info['count']:0;
            $list[$k]['is_one'] = isset($info['is_one'])?$info['is_one']:0;
        }
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '主播列表';
        $this->display();
    }

    /**
     * 开启一对一直播修改模板
     * */
    public function open_is_one(){
        $id = $_GET['id'];

        $this->assign('id',$id);
        $this->display();
    }

    /**
     * 开启一对一直播
     * */
    public function open_is_one_ok(){
        $id = $_POST['id'];
        $blance = $_POST['blance'];

        $res = M('anchor')->where(['auid' => $id])->save(['is_one' => 1,'blance' => $blance]);

        if($res){
            $this->success("操作成功",U("anchor"));
        }else{
            $this->error("操作失败",U("anchor"));
        }
    }

    /**
     * 关闭一对一直播
     * */
    public function close_is_one(){
        $id = $_GET['id'];

        $res = M('anchor')->where(['auid' => $id])->save(['is_one' => 0,'blance' => 0]);

        if($res){
            $this->success("操作成功",U("anchor"));
        }else{
            $this->error("操作失败",U("anchor"));
        }

    }

    //认证详情
    public function suthentic_detail(){
        $where['id']=$_GET['id'];
        $info=M('user')->where($where)->find();
        $map['id']=$info['skill_id'];
        $info['skill']=M('skill')->where($map)->getField('name');
        $levels=M('gz_level')->select();
        $this->assign('levels',$levels);
        $this->assign('info',$info);
        $this->meta_title = '认证详情';
        $this->display();
    }


    //主播详情
    public  function anchor_detail(){
        $where['id']=$_GET['id'];
        $info=M('user')->where($where)->find();
        $map['id']=$info['skill_id'];
        $info['skill']=M('skill')->where($map)->getField('name');
        $info['level']=exp_rank($info['exp']);
        $this->assign('info',$info);
        $this->meta_title = '主播详情';
        $this->display();

    }
    public function show(){
        $where['id']=$_GET['id'];
        $info=M('user')->field('number,cover')->where($where)->find();
        $playurl=getPlayUrl1($info['number']);
        $cover=getImgurl($info['cover']);
        $this->assign('flv',$playurl[1]);
        $this->assign('playurl',$playurl[2]);
        $this->assign('cover',$cover);
        $this->meta_title = '直播';
        $this->display();
    }

    //设置主播头衔
    public function set_level(){
        $where['id']=$_POST['id'];
        $data['level']=$_POST['level'];
        $res=M('user')->where($where)->save($data);
        if($res){
            $this->success("设置成功",U("suthentic"));
        }else{
            $this->error("设置失败");
        }
    }

    //认证审核
    public function check_rz(){
        $where['id']=$_GET['id'];
        $data['rz_status']=$_GET['status'];
        if($_GET['status']==2){
            $data['is_anchor']=1;
            $data['rz_status']=3;
            $data['anchar_time']=time();
        }

        if($_GET['status']==3){
            $data['rz_status']=2;
        }

        $res=M('user')->where($where)->save($data);
        if($res){
            $this->success("审核成功",U("suthentic"));
        }else{
            $this->error("审核失败");
        }
    }


    //歌曲列表
    public function music(){
        $list=M('music')->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('music')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '歌曲列表';
        $this->display();
    }

    //新增
    public function music_add(){
        if($_POST){
            if(!$_REQUEST['title']){
                $this->error("请输入歌名");
            }
            if(!$_REQUEST['yc_url'] && !$_REQUEST['bz_url']){
                $this->error("请上传原唱或伴奏");
            }


            $data['title']=$_REQUEST['title'];
            $data['yc_url']=$_REQUEST['yc_url'];
            $data['bz_url']=$_REQUEST['bz_url'];
            $data['title']=$_REQUEST['title'];
            $data['create_time']=time();
            $res=M('music')->add($data);
            if($res){
                $this->success("添加成功",U("music"));
            }else{
                $this->error("添加失败");
            }
        }else{
            $this->display();
        }
    }

    //修改音乐
    public function music_edit(){
        if($_POST){
            if(!$_REQUEST['title']){
                $this->error("请输入歌名");
            }

            if(!$_REQUEST['yc_url'] && !$_REQUEST['bz_url']){
                $this->error("请上传原唱或伴奏");
            }

            $data['title']=$_REQUEST['title'];
            $data['yc_url']=$_REQUEST['yc_url'];
            $data['bz_url']=$_REQUEST['bz_url'];
            $data['title']=$_REQUEST['title'];
            $where['id']=$_REQUEST['id'];
            $res=M('music')->where($where)->save($data);
            if($res){
                $this->success('编辑成功！', U('music'));
            }else{
                $this->error('编辑失败');
            }
        }else{
            $info = M('music')->where(['id' => $_GET['id']])->find();
            $this->assign('info', $info);
            $this->meta_title = '编辑音乐';
            $this->display();
        }
    }

    //删除音乐
    public  function del_music(){
        $id=$_GET['id'];
        $del=M('music')->where('id='.$id)->delete();
        if($del){
            $this->success('删除成功！', U('music'));
        }else{
            $this->error('删除失败');
        }
    }

    //礼物列表
    public function gift(){
        $list=M('gift')->order('id desc')->where("status!=2")->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('gift')->limit($Page->firstRow.','.$Page->listRows)->where("status!=2")->select();
        foreach($list as $k=>$v){
            $where['id']=$v['cate_id'];
            $list[$k]['cate_name']=M('giftcate')->where($where)->getField('name');
        }
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '礼物列表';
        $this->display();
    }

    //新增,编辑礼物
    public function gift_add(){
        header('content-type:text/html;charset=utf8');
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入礼物名称");
            }
            if(!$_POST['cate_id']){
                $this->error("请选择礼物分类");
            }
            if($_POST['value'] == ''){
                $this->error("请输入钻石数量");
            }
            if(!$_POST['img']){
                $this->error("请上传图片");
            }

            $data['name']=$_POST['name'];
            $data['cate_id']=$_POST['cate_id'];
            $data['value']=$_POST['value'];
            $data['img']=$_POST['img'];
            $data['is_send']=$_POST['is_send'];
            if($_POST['special_img']) $data['special_img']=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/Uploads/svga/'.$_POST['special_img'];
            if($_POST['id']){
                $res=M('gift')->where("id=".$_POST['id'])->save($data);

                if($res){
                    $this->success("修改成功",U("gift"));
                }else{
                    $this->error("修改失败");
                }
            }else{
                $data['create_time']=time();
                $res=M('gift')->add($data);
                if($res){
                    $this->success("添加成功",U("gift"));
                }else{
                    $this->error("添加失败");
                }
            }
        }else{
            $list=M('giftcate')->where("status!=2")->select();
            $this->assign('cates',$list);
            if($_GET['id']){
                $info=M('gift')->where("id=".$_GET['id'])->find();
                $info['special_img'] = str_replace($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/Uploads/svga/','',$info['special_img']);
                $this->assign('info',$info);
            }
            $this->display();
        }
    }


    //删除礼物
    public function gift_del(){
        $map['id']=$_GET['id'];
        $data['status']=2;
        $res=M('gift')->where($map)->save($data);
        if($res){
            $this->success("删除成功",U("gift"));
        }else{
            $this->error("删除失败");
        }
    }


    //钻石充值列表
    public function diamond(){
        $list=M('diamond')->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('diamond')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '钻石列表';
        $this->display();
    }



    //添加钻石列表
    public function diamond_add(){
        if($_POST){
            if(!$_POST['num']){
                $this->error("请输入钻石数量");
            }
            if(!$_POST['price']){
                $this->error("请输入价格");
            }
            $data['num']=$_POST['num'];
            $data['price']=$_POST['price'];
            $data['type']=1;
            if($_POST['id']){
                $where['id']=$_POST['id'];
                $res=M('diamond')->where($where)->save($data);
            }else{
                $res=M('diamond')->add($data);
            }

            if($res){
                $this->success("操作成功",U("diamond"));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->meta_title = '添加套餐';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('diamond')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除钻石
    public  function  diamond_del(){
        $map['id']=$_GET['id'];
        $res=M('diamond')->where($map)->delete();
        if($res){
            $this->success("删除成功",U("diamond"));
        }else{
            $this->error("删除失败");
        }
    }

    //充值记录
    public function recharge(){
        $dataArr = cookie('recharge_search');
        if(!$_REQUEST['types']){
            cookie('recharge_search',null);
        }else{
            $dataArr = cookie('recharge_search');
        }

        if($dataArr){
            if($dataArr['uid']){
                $where['uid']=$dataArr['uid'];
                $this->assign('uid',$dataArr['uid']);
            }
            $start_time=$dataArr['start_time'];
            $end_time=$dataArr['end_time'];
            if($end_time>0 && $start_time>0){
                $this->assign('start_time',$start_time);
                $this->assign('end_time',$end_time);
                $where['create_time'] = array('BETWEEN',array(strtotime($start_time),strtotime($end_time)));
            }
        }



        $list=M('recharge')->where($where)->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('recharge')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
       // echo M('recharge')->getLastSql();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '充值记录';
        $this->display();
    }

    //兑换记录
    public function change(){
        $dataArr = cookie('change_search');
        if(!$_REQUEST['types']){
            cookie('change_search',null);
        }else{
            $dataArr = cookie('change_search');
        }

        if($dataArr){
            $start_time=$dataArr['start_time'];
            $end_time=$dataArr['end_time'];
            if($end_time>0 && $start_time>0){
                $this->assign('start_time',$start_time);
                $this->assign('end_time',$end_time);
                $where['create_time'] = array('BETWEEN',array(strtotime($start_time),strtotime($end_time)));
            }
        }

        $list=M('change')->where($where)->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('change')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '兑换记录';
        $this->display();
    }

    //使用记录
    public function use_record(){
        $dataArr = cookie('use_record_search');
        if(!$_REQUEST['types']){
            cookie('use_record_search',null);
        }else{
            $dataArr = cookie('use_record_search');
        }

        if($dataArr){
            if($dataArr['uid']){
                $where['uid']=$dataArr['uid'];
                $this->assign('uid',$dataArr['uid']);
            }
            $start_time=$dataArr['start_time'];
            $end_time=$dataArr['end_time'];
            if($end_time>0 && $start_time>0){
                $this->assign('start_time',$start_time);
                $this->assign('end_time',$end_time);
                $where['create_time'] = array('BETWEEN',array(strtotime($start_time),strtotime($end_time)));
            }
        }

        $list=M('record')->where($where)->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('record')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '使用记录';
        $this->display();
    }

    //推送列表
    public function message(){
        $list=M('message')->where("type=1")->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('message')->where("type=1")->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '消息列表';
        $this->display();
    }


    //添加消息推送
    public function message_add(){
        if($_POST){
            if(!$_POST['title']){
                $this->error("请输入标题");
            }
            if(!$_POST['content']){
                $this->error("请输入内容");
            }
            $data['title']=$_POST['title'];
            $data['content']=$_POST['content'];
            $data['type']=1;
            if($_POST['uid']){
                $data['uid']=$_POST['uid'];
                if($_POST['uid'] != 0){
                    //推送给指定用户
                    $push_res_json = single_push($_POST['content'],$_POST['uid']);
                    $push_res = json_decode($push_res_json,true);
                    if($push_res['ActionStatus'] != 'OK'){
                        $this->error('推送发生错误');
                    }
                }else{
                    //推送给所用用户
                    $push_res_json = all_push($_POST['content']);
                    $push_res = json_decode($push_res_json,true);
                    if($push_res['ActionStatus'] != 'OK'){
                        $this->error('推送发生错误');
                    }
                }
            }

            $data['create_time']=time();
            $res=M('message')->add($data);

            if($res){
                $this->success("新增成功",U("message"));
            }else{
                $this->error("新增失败");
            }
        }else{
            $this->meta_title = '消息列表';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('message')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }

    //删除消息
    public  function  message_del(){
        $map['id']=$_GET['id'];
        $res=M('message')->where($map)->delete();
        if($res){
            $this->success("删除成功",U("message"));
        }else{
            $this->error("删除失败");
        }
    }


    //广告列表
    public function advert(){
        $list=M('advert')->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('advert')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '广告列表';
        $this->display();
    }

    //新增,编辑广告
    public function advert_add(){
        if($_POST){
            if($_POST['type_url']==1){
                if(!$_POST['link_id']){
                    $this->error("请选择主播");
                }
                $data['link_url']='';
                $data['link_id']=$_POST['link_id'];
            }
            if($_POST['type_url']==2){
                if(!$_POST['link_url']){
                    $this->error("请填写链接地址");
                }
                $data['link_id']=0;
                $data['link_url']=$_POST['link_url'];
            }
            if(!$_POST['img']){
                $this->error("请上传图片");
            }

            $data['img']=$_POST['img'];
            if($_POST['id']){
                $res=M('advert')->where("id=".$_POST['id'])->save($data);
                if($res){
                    $this->success("修改成功",U("advert"));
                }else{
                    $this->error("修改失败");
                }
            }else{
                $res=M('advert')->add($data);
                if($res){
                    $this->success("添加成功",U("advert"));
                }else{
                    $this->error("添加失败");
                }
            }
        }else{
            $list=M('user')->field('id,nickname')->where("is_anchor=1")->select();

            $this->assign('cates',$list);
            if($_GET['id']){
                $info=M('advert')->where("id=".$_GET['id'])->find();
                $this->assign('info',$info);
            }
            $this->display();
        }
    }


    //删除广告
    public  function advert_del(){
        $id=$_GET['id'];
        $del=M('advert')->where('id='.$id)->delete();
        if($del){
            $this->success('删除成功！', U('advert'));
        }else{
            $this->error('删除失败');
        }
    }

    //等级列表
    public function level(){
        $list=M('gz_level')->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('gz_level')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '等级列表';
        $this->display();
    }


    //添加,修改等级
    public function level_add(){
        if($_POST){
            if(!$_POST['name']){
                $this->error("请输入名称");
            }
            if(!$_POST['gift_ids']){
                $this->error("请选择关联礼物");
            }
            $data['name']=$_POST['name'];
            $data['gift_ids']=implode(',',$_POST['gift_ids']);;
            $data['type']=2;
            $data['img']=$_POST['img'];
            if($_POST['id']){
                $res=M('gz_level')->where("id=".$_POST['id'])->save($data);
                if($res){
                    $this->success("新增成功",U("level"));
                }else{
                    $this->error("新增失败");
                }
            }else{
                $res=M('gz_level')->add($data);
                if($res){
                    $this->success("添加成功",U("level"));
                }else{
                    $this->error("添加失败");
                }
            }
        }else{
            $this->meta_title = '添加等级';
            $gifts=M('gift')->field('id,name')->select();
            $this->assign('gifts',$gifts);
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('gz_level')->where($map)->find();
                $gift_ids=explode(",",$res['gift_ids']);
                $this->assign('gift_ids',$gift_ids);
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除等级
    public function level_del(){
        $id=$_GET['id'];
        $del=M('gz_level')->where('id='.$id)->delete();
        if($del){
            $this->success('删除成功！', U('level'));
        }else{
            $this->error('删除失败');
        }
    }

    //礼包列表
    public function giftbag(){
        $list=M('giftbag')->order('id desc')->where('status=1')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('giftbag')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->where('status=1')->select();

        foreach($list as $k=>$v){
            $list[$k]['level']=M('gz_level')->where("id=".$v['level_id'])->getField('name');

            if($v['user_level']>0){
                $list[$k]['user_level']=M('gz_level')->where("id=".$v['user_level'])->getField('name');
            }else{
                $list[$k]['user_level']='所有用户';
            }
        }

        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '漂流瓶列表';
        $this->display();
    }


    //新增，编辑礼包
    public function giftbag_add(){
        if($_POST){
            if(!$_POST['type']){
                $this->error("请选择漂流瓶类型");
            }
            if(!$_POST['price']){
                $this->error("请输入金额");
            }

            if($_POST['type'] == 1){
                if(!$_POST['zs_num']){
                    $this->error("请输入钻石数");
                }
                $data['zs_num']=$_POST['zs_num'];
                $push_text = '您收到一个钻石漂流瓶';
            }else{
                if(!$_POST['day']){
                    $this->error("请输入天数");
                }
                if(!$_POST['level_id']){
                    $this->error("请选择贵族vip等级");
                }
                $data['day']=$_POST['day'];
                $data['level_id']=$_POST['level_id'];
                $push_text = '您收到一个贵族漂流瓶';
            }

            $data['type']=$_POST['type'];//漂流瓶类型
            $data['price']=$_POST['price'];
            $data['user_level']=$_POST['user_level'];

            $data['create_time']=time();

            if($_POST['user_level'] == 0){
                //推送给所有用户
                $push_res_json = all_push($push_text);
                $push_res = json_decode($push_res_json,true);
                if($push_res['ActionStatus'] != 'OK'){
                    $this->error('推送发生错误');
                }
            }else{
                //推送给指定vip用户
                $push_res_json = single_push_vip($push_text,$_POST['user_level']);
                $push_res = json_decode($push_res_json,true);
                if($push_res['ActionStatus'] != 'OK'){
                    $this->error('推送发生错误');
                }
            }

            if($_POST['id']){
                $res=M('giftbag')->where("id=".$_POST['id'])->save($data);
                if($res){
                    $this->success("编辑成功",U("giftbag"));
                }else{
                    $this->error("编辑失败");
                }
            }else{
                $res=M('giftbag')->add($data);
                if($res){
                    $this->success("添加成功",U("giftbag"));
                }else{
                    $this->error("添加失败");
                }
            }
        }else{
            $levels=M('gz_level')->select();
            $this->assign('levels',$levels);
            $this->meta_title = '新增漂流瓶';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('giftbag')->where($map)->find();
                $this->assign('info',$res);
                $type = count($res)>0?$res['type']:0;
                $this->assign('type', $type);
            }else{
                $type = 0;
            }
            $this->assign('type', $type);

            $this->display();
        }
    }


    //删除漂流瓶
    public function giftbag_del(){
        $id=$_GET['id'];
        $data['status']=2;
        $del=M('giftbag')->where('id='.$id)->save($data);
        if($del){
            $this->success('删除成功！', U('giftbag'));
        }else{
            $this->error('删除失败');
        }
    }


    //提现列表
    public function withdraw(){
        $dataArr = cookie('withdraw_search');
        if(!$_REQUEST['types']){
            cookie('withdraw_search',null);
        }else{
            $dataArr = cookie('withdraw_search');
        }

        if($dataArr){
            $start_time=$dataArr['start_time'];
            $end_time=$dataArr['end_time'];
            if($end_time>0 && $start_time>0){
                $this->assign('start_time',$start_time);
                $this->assign('end_time',$end_time);
                $where['create_time'] = array('BETWEEN',array(strtotime($start_time),strtotime($end_time)));
            }
        }

        $list=M('withdrawals')->where($where)->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('withdrawals')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '提现列表';
        $this->display();
    }

    //提现审核
    public function check_withdraw(){
        $where['id']=$_GET['id'];
        $data['status']=$_GET['status'];
        if($_GET['status']==2){  //失败
              M('user')->where("id=".$_GET['uid'])->setInc('profit',$_GET['profit']);
        }
        $res=M('withdrawals')->where($where)->save($data);
        if($res){
            $this->success("审核成功",U("withdraw"));
        }else{
            $this->error("审核失败");
        }
    }


    //送礼记录
    public function sendgift(){
        $dataArr = cookie('sendgift_search');
        if(!$_REQUEST['types']){
            cookie('sendgift_search',null);
        }else{
            $dataArr = cookie('sendgift_search');
        }

        if($dataArr){
            if($dataArr['uid']){
                $is_anchor=M('user')->where("id=".$dataArr['uid'])->getField('is_anchor');
                if($is_anchor==1){
                    $where['auid']=$dataArr['uid'];  //主播
                }else{
                    $where['uid']=$dataArr['uid'];   //普通用户
                }
                $this->assign('uid',$dataArr['uid']);
            }

            $start_time=$dataArr['start_time'];
            $end_time=$dataArr['end_time'];
            if($end_time>0 && $start_time>0){
                $this->assign('start_time',$start_time);
                $this->assign('end_time',$end_time);
                $where['create_time'] = array('BETWEEN',array(strtotime($start_time),strtotime($end_time)));
            }
        }

        $list=M('send_gift')->where($where)->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('send_gift')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
      //  echo M('send_gift')->getLastSql();
        foreach($list as $k=>$v){
            $list[$k]['zs_num']=$v['num']*$v['zs_num'];
        }
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '礼物';
        $this->display();
    }



    //用户列表
    public function user(){
        $dataArr = cookie('user_search');
        if(!$_REQUEST['types']){
            cookie('user_search',null);
        }else{
            $dataArr = cookie('user_search');
        }
        $nickname=$dataArr['nickname'];
        if($nickname){
            $this->assign('nickname',$nickname);
            $where['nickname']=array('like','%'.$dataArr['nickname'].'%');
        }
//        $where['is_anchor']=array('eq',0);
        $list=M('user')->where($where)->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('user')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();


        foreach($list as $k=>$v){
            $list[$k]['user_level']=exp_rank($v['exp']);
    //        $list[$k]['level']=M('gz_')
        }


        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '用户列表';
        $this->display();
    }

    //守护时长设置
    public function set_sh_time(){

    }

    //贵族时长设置
    public function set_gz_time(){

    }


    //用户信息
    public function user_detail(){
        $where['id']=$_GET['id'];
        $info=M('user')->where($where)->find();
        $map['id']=$info['skill_id'];
        $info['skill']=M('skill')->where($map)->getField('name');
        $info['level']=M('gz_level')->where($map)->getField('name');
        $this->assign('info',$info);
        $this->meta_title = '用户信息';
        $this->display();
    }


    //申诉列表
   public function appeal(){
       $dataArr = cookie('appeal_search');
       if(!$_REQUEST['types']){
           cookie('appeal_search',null);
       }else{
           $dataArr = cookie('appeal_search');
           if(!$dataArr['start_time'] && !$dataArr['end_time']){
               cookie('appeal_search',null);
           }
       }


       if($dataArr){
           $start_time=$dataArr['start_time'];
           $end_time=$dataArr['end_time'];
           if($end_time>0 && $start_time>0){
               $this->assign('start_time',$start_time);
               $this->assign('end_time',$end_time);
               $where['create_time'] = array('BETWEEN',array(strtotime($start_time),strtotime($end_time)));
           }
       }


       $where['sx_time']=array('gt',0);
       $list=M('drawback')->where($where)->order('id desc')->select();
       $count      =  count($list);// 查询满足要求的总记录数
       $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
       $show       = $Page->show();// 分页显示输出
       $list=M('drawback')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
      // echo M('drawback')->getLastSql();
       $this->assign('list',$list);
       $this->assign('_page',$show);
       $this->meta_title = '申诉列表';
       $this->display();
   }

    //申诉详情
    public function appeal_detail(){
        $where['id']=$_GET['id'];
        $info=M('drawback')->where($where)->find();
        $info['type']=M('refusecate')->where("id=".$info['type_id'])->getField('name');
        $this->assign('info',$info);
        $this->meta_title = '申诉详情';
        $this->display();
    }


    //申诉审核
    public function appeal_rz(){
        $where['id']=$_GET['id'];
        $data['rz_status']=$_GET['status'];
        if($_GET['status']==2){
            $data['status']=2;  //同意申诉，不同意退货退款
            $data['sx_status']=2; //申诉成功
            $odata['order_status']=10;
        }

        if($_GET['status']==3){
            $data['status']=1;  //不同意申诉，同意退货退款
            $data['sx_status']=3;  //申诉失败
            if($_GET['is_refund']==1){
                $uwhere['id']=$_GET['uid'];
                M('user')->where($uwhere)->setInc('zs_blance',$_GET['zs_num']);
            }
            $odata['order_status']=9;
        }


        $res=M('drawback')->where($where)->save($data);
        if($res){
            M('order')->where("id=".$_GET['oid'])->save($odata);
            $this->success("审核成功",U("appeal"));
        }else{
            $this->error("审核失败");
        }
    }



    //任务列表
    public function task(){
        $list=M('task')->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('task')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = '任务列表';
        $this->display();
    }

    //添加,修改任务
    public function task_add(){
        if($_POST){
            if(!$_POST['hour']){
                $this->error("请输入时间");
            }
            if(!$_POST['rate']){
                $this->error("请输入收益");
            }


            if(!isint($_POST['hour']) || $_POST['hour']>24){
                $this->error("请输入正确的小时数");
            }
            if(!isint($_POST['rate']) || $_POST['rate']>99){
                $this->error("收益格式不正确");
            }
            $data['hour']=$_POST['hour'];
            $data['rate']=$_POST['rate'];

            if($_POST['id']){
                $res=M('task')->where("id=".$_POST['id'])->save($data);
                if($res){
                    $this->success("新增成功",U("task"));
                }else{
                    $this->error("新增失败");
                }
            }else{
                $data['create_time']=time();
                $res=M('task')->add($data);
                if($res){
                    $this->success("添加成功",U("task"));
                }else{
                    $this->error("添加失败");
                }
            }
        }else{
            $this->meta_title = '添加任务';
            if($_GET['id']){
                $map['id']=$_GET['id'];
                $res=M('task')->where($map)->find();
                $this->assign('info',$res);
            }
            $this->display();
        }
    }


    //删除任务
    public  function  task_del(){
        $map['id']=$_GET['id'];
        $res=M('task')->where($map)->delete();
        if($res){
            $this->success("删除成功",U("task"));
        }else{
            $this->error("删除失败");
        }
    }


    public function pk(){
        $list=M('pk')->order('status desc,id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('pk')->limit($Page->firstRow.','.$Page->listRows)->order('status desc,id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = 'pk列表';
        $this->display();
    }


    public function pk_set(){
        /*$config = api('Config/lists');
        C($config); //添加配置
        $this->assign('is_pk',C('IS_PK'));
        $this->meta_title = 'pk开关设置';*/

        $list=M('pk_set')->order('id desc')->select();
        $count      =  count($list);// 查询满足要求的总记录数
        $Page       = new \Think\Page($count, C('LIST_ROWS'));// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list=M('pk_set')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->meta_title = 'pk设置';
        $this->display();
    }

    public function edit_pk_time(){
        $id = $_GET['id'];

        $info = M('pk_set')->where(['id' => $id])->find();

        $this->assign('info',$info);
        $this->display();
    }

    public function eidt_pk_time_ok(){
        $start_time = str_replace('：',':',$_POST['start_time']);
        $end_time = str_replace('：',':',$_POST['end_time']);

        $data = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'pk_time' => $_POST['pk_time'],
        ];

        $res = M('pk_set')->where(['id' => $_POST['id']])->save($data);
        if($res){
            $this->success('修改成功',U("pk_set"));
        }else{
            $this->error('修改失败或者未修改数据');
        }
    }

    public function set_pk_status(){
        $value = $_REQUEST['value'];
        $res = M('config')->where(['name' => 'IS_PK'])->setField(['value' => $value]);
        echo $res;
    }


    //主播筛选
    public function  anchor_search(){
        $data['nickname']=$_POST['nickname'];
        cookie('anchor_search',$data);
        $this->redirect('Manage/anchor',array('types'=>'1'));
    }


    //用户搜索
    public function  user_search(){
        $data['nickname']=$_POST['nickname'];
        cookie('user_search',$data);
        $this->redirect('Manage/user',array('types'=>'1'));
    }


    //充值记录搜索
    public function  recharge_search(){
        $data['start_time']=$_POST['start_time'];
        $data['end_time']=$_POST['end_time'];
        $data['uid']=$_REQUEST['id'];
        cookie('recharge_search',$data);
        $this->redirect('Manage/recharge',array('types'=>'1'));
    }

    //消费记录搜索
    public function  use_record_search(){
        $data['start_time']=$_POST['start_time'];
        $data['end_time']=$_POST['end_time'];
        $data['uid']=$_REQUEST['id'];
        cookie('use_record_search',$data);
        $this->redirect('Manage/use_record',array('types'=>'1'));
    }


    //申诉搜索
    public function  appeal_search(){
        $data['start_time']=$_POST['start_time'];
        $data['end_time']=$_POST['end_time'];
        cookie('appeal_search',$data);
        $this->redirect('Manage/appeal',array('types'=>'1'));
    }


    //送礼记录搜索
    public function  sendgift_search(){
        $data['start_time']=$_POST['start_time'];
        $data['end_time']=$_POST['end_time'];
        $data['uid']=$_REQUEST['id'];
        cookie('sendgift_search',$data);
        $this->redirect('Manage/sendgift',array('types'=>'1'));
    }

    //提现搜索
    public function  withdraw_search(){
        $data['start_time']=$_POST['start_time'];
        $data['end_time']=$_POST['end_time'];
        cookie('withdraw_search',$data);
        $this->redirect('Manage/withdraw',array('types'=>'1'));
    }

    //兑换记录搜索
    public function  change_search(){
        $data['start_time']=$_POST['start_time'];
        $data['end_time']=$_POST['end_time'];
        cookie('change_search',$data);
        $this->redirect('Manage/change',array('types'=>'1'));
    }

}
